package com.jprocms.module.cms.controller.directive;

import com.jpro.framework.common.enums.CommonStatusEnum;
import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.collection.CollectionUtils;
import com.jpro.module.system.api.notice.NoticeApi;
import com.jpro.module.system.api.notice.dto.NoticePageReqDTO;
import com.jpro.module.system.api.notice.dto.NoticeRespDTO;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagExportReqVO;
import com.jprocms.module.cms.controller.admin.content.tagvo.TagPageReqVO;
import com.jprocms.module.cms.dal.dataobject.content.TagDO;
import com.jprocms.module.cms.service.content.TagService;
import com.jprocms.module.cms.util.Freemarkers;
import com.jprocms.module.cms.util.FrontContextUtil;
import com.jprocms.module.cms.web.Directives;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 通知公告列表 自定义标签
 *
 * @author jprocms
 */
public class NoticeListDirective implements TemplateDirectiveModel {
    /**
     * 公告类型，参见 NoticeTypeEnum 枚举类  1通知 2公告
     */
    public static final String TYPE = "type";
    /**
     * 标题。字符串(String)。
     */
    public static final String TITLE = "title";

    protected void doExecute(Environment env, Map<String, TemplateModel> params, TemplateModel[] loopVars,
                             TemplateDirectiveBody body, boolean isPage) throws TemplateException, IOException {
        Freemarkers.requireLoopVars(loopVars);
        Freemarkers.requireBody(body);
        Integer type = Directives.getInteger(params, TYPE);
        String title = Directives.getString(params, TITLE);
        NoticePageReqDTO pageReqVO = new NoticePageReqDTO();
        pageReqVO.setTitle(title);
        pageReqVO.setType(type);
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        if (isPage) {
            int page = Directives.getPage(params, env);
            int pageSize = Directives.getPageSize(params, env);
            pageReqVO.setPageNo(page);
            pageReqVO.setPageSize(pageSize);
            final CommonResult<PageResult<NoticeRespDTO>> noticePageResult = service.getNoticePage(pageReqVO);
            if(noticePageResult.isSuccess()){
                PageResult<NoticeRespDTO> noticePage = noticePageResult.getData();
                noticePage.adjustPages(pageReqVO);
                Directives.setTotalPages(noticePage.getPages());
                loopVars[0] = env.getObjectWrapper().wrap(noticePage);
            }else{
                loopVars[0] = env.getObjectWrapper().wrap(new PageResult(new ArrayList(),0L,0L,0L));
            }
        } else {
            pageReqVO.setPageSize(Directives.getCount(params));
            final CommonResult<List<NoticeRespDTO>> noticeListResult = service.getNoticeList(pageReqVO);
            if(noticeListResult.isSuccess()){
                final List<NoticeRespDTO> list = noticeListResult.getData();
                loopVars[0] = env.getObjectWrapper().wrap(list);
            }else{
                loopVars[0] = env.getObjectWrapper().wrap(new ArrayList<>());
            }
        }
        body.render(env.getOut());
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        doExecute(env, params, loopVars, body, false);
    }


    private final NoticeApi service;

    public NoticeListDirective(NoticeApi service) {
        this.service = service;
    }
}
